{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Using the RelevantFeatureAugmenter with separate datasets for train and test data\n",
    "\n",
    "This notebook illustrates the RelevantFeatureAugmenter in pipelines where you have first train on samples from dataset `df_train` but then want to test using samples from another `df_test`.\n",
    "(Here `df_train` and `df_test` refer to the dataframes that contain the time series data)\n",
    "\n",
    "Due to limitations in the sklearn pipeline API one has to use the `ppl.set_params(fresh__timeseries_container=df)` method for those two dataframes between train and test run."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/max/Documents/tsfresh/venv3/lib/python3.6/site-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.\n",
      "  \"This module will be removed in 0.20.\", DeprecationWarning)\n",
      "/Users/max/Documents/tsfresh/venv3/lib/python3.6/site-packages/statsmodels/compat/pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.\n",
      "  from pandas.core import datetools\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import classification_report\n",
    "from tsfresh.examples.robot_execution_failures import download_robot_execution_failures\n",
    "from tsfresh.examples import load_robot_execution_failures\n",
    "from tsfresh.feature_extraction.settings import MinimalFCParameters\n",
    "from tsfresh.transformers import RelevantFeatureAugmenter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1320, 8)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "download_robot_execution_failures()\n",
    "df, y = load_robot_execution_failures()\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((990, 8), (330, 8))"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Here, df contains the time series of both train and test set. \n",
    "# We will split it into a train df_train and a test set  df_test:\n",
    "y_train, y_test = train_test_split(y)\n",
    "df_train = df.loc[df.id.isin(y_train.index)]\n",
    "df_test = df.loc[df.id.isin(y_test.index)]\n",
    "X_train = pd.DataFrame(index=y_train.index)\n",
    "X_test = pd.DataFrame(index=y_test.index)\n",
    "df_train.shape, df_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "ppl = Pipeline([('fresh', RelevantFeatureAugmenter(column_id='id', column_sort='time', \n",
    "                                                   default_fc_parameters=MinimalFCParameters())),\n",
    "                ('clf', RandomForestClassifier())])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Feature Extraction: 100%|██████████| 396/396 [00:00<00:00, 37120.22it/s]\n",
      "WARNING:tsfresh.feature_selection.feature_selector:[test_feature_significance] Feature T_z__length is constant\n",
      "WARNING:tsfresh.feature_selection.feature_selector:[test_feature_significance] Feature F_y__length is constant\n",
      "WARNING:tsfresh.feature_selection.feature_selector:[test_feature_significance] Feature F_x__length is constant\n",
      "WARNING:tsfresh.feature_selection.feature_selector:[test_feature_significance] Feature T_y__length is constant\n",
      "WARNING:tsfresh.feature_selection.feature_selector:[test_feature_significance] Feature T_x__length is constant\n",
      "WARNING:tsfresh.feature_selection.feature_selector:[test_feature_significance] Feature F_z__length is constant\n",
      "Feature Extraction: 100%|██████████| 396/396 [00:00<00:00, 197496.36it/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Pipeline(steps=[('fresh', RelevantFeatureAugmenter(chunksize=None, column_id=None, column_kind=None,\n",
       "             column_sort=None, column_value=None,\n",
       "             default_fc_parameters=None, disable_progressbar=None,\n",
       "             fdr_level=None, filter_only_tsfresh_features=True,\n",
       "             hypotheses_ind...imators=10, n_jobs=1, oob_score=False, random_state=None,\n",
       "            verbose=0, warm_start=False))])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# for the fit on the train test set, we set the fresh__timeseries_container to `df_train`\n",
    "ppl.set_params(fresh__timeseries_container=df_train)\n",
    "ppl.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Feature Extraction: 100%|██████████| 132/132 [00:00<00:00, 449025.25it/s]\n"
     ]
    }
   ],
   "source": [
    "# for the predict on the test test set, we set the fresh__timeseries_container to `df_test`\n",
    "ppl.set_params(fresh__timeseries_container=df_test)\n",
    "y_pred = ppl.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             precision    recall  f1-score   support\n",
      "\n",
      "          0       1.00      1.00      1.00         5\n",
      "          1       1.00      1.00      1.00        17\n",
      "\n",
      "avg / total       1.00      1.00      1.00        22\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(classification_report(y_test, y_pred))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
